import { fromTemplate, mergeTemplates } from '$lib/builder';
import { Cover, Description, Feature, Glyph, Opaque, type Entity } from '$lib/components';
import { defineComponent, type IWorld } from '$lib/ecs';
import { HP, hurt, registerCorpseDrop } from './stats';
import { getPos3, placeOnMap } from '$lib/pos3';

export const Bush = defineComponent('Bush', {});

export const mowLeaves = (world: IWorld, e: Entity, amount: number) => {
  return hurt(world, e, amount);
};

export const treeTemplate = [
  [Feature],
  [Glyph, {
    glyph: '7',
    color: '#3a3',
  }],
  [Description, {
    summary: "tree",
    description: "Generic high-rise plant.",
  }],
  [Opaque],
  [Cover, {
    size: 10,
  }],
];

export const appleTreeTemplate = mergeTemplates(treeTemplate, [
  [Glyph, {
    color: '#a71',
  }],
  [Description, {
    summary: "apple tree",
    description: "High-rise plant that drops those tasty crunchy fruits in the autumn.",
  }],
]);

export const bushTemplate = [
  [Feature],
  [Bush],
  [Glyph, {
    glyph: ';',
    color: '#3a3',
  }],
  [Description, {
    summary: "bush",
    description: "Decently large plant. You can probably hide here.",
  }],
  [HP, {
    maxhp: 7,
    hp: 7,
  }],
];

export const deadBushTemplate = [
  [Feature],
  [Glyph, {
    glyph: ';',
    color: '#877',
  }],
  [Description, {
    summary: "dead bush",
    description: "Decently large plant. It looks dead though.",
  }],
];

export const grassTemplate = [
  [Feature],
  [Glyph, {
    glyph: '.',
    color: '#4a1',
  }],
  [Description, {
    summary: "grass",
    description: "Small green foliage. Feels nice to tread through.",
  }],
];

registerCorpseDrop({
  target: Bush,
  drop: (world, e) => {
    const {x, y, z} = getPos3(e);
    const deadBush = fromTemplate(world, deadBushTemplate);
    placeOnMap(world, deadBush, x, y, z);
  }
});
